package com.flyme.module.base.provider.controller;

import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysMenuRemoteService;
import com.flyme.module.base.client.entity.SystemMenu;
import com.flyme.module.base.provider.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zyf
 */
@Api(tags = "菜单资源管理")
@RestController
public class SysMenuController extends BaseController<SysMenuService, SystemMenu> implements SysMenuRemoteService {


    /**
     * 菜单列表
     *
     * @return
     */
    @ApiOperation(value = "菜单列表", notes = "菜单列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "pagerInfo", value = "分页参数", paramType = "form"), @ApiImplicitParam(name = "keyword", value = "查询字段", paramType = "form"),})
    @PostMapping("/menu")
    @Override
    public PqGrid menu(@RequestParam(value = "pagerInfo") PagerInfo pagerInfo, @RequestParam(name = "keyword", required = false) String keyword) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.like("title", keyword).or().like("menuCode", keyword);
        cq.orderByAsc("id", "priority");
        return bizService.pageList(cq);
    }


    /**
     * 获取菜单资源
     *
     * @param menuId 应用menuId
     * @return 应用信息
     */
    @ApiOperation(value = "获取菜单资源", notes = "获取菜单资源")
    @ApiImplicitParams({@ApiImplicitParam(name = "menuId", required = true, value = "menuId"),})
    @GetMapping("/menu/{menuId}")
    @Override
    public ResultBody getMenu(@PathVariable("menuId") Long menuId) {
        return ResultBody.success(bizService.getById(menuId));
    }

    /**
     * 添加菜单资源
     *
     * @param menu 菜单实体
     * @return
     */
    @ApiOperation(value = "添加菜单资源", notes = "添加菜单资源")
    @PostMapping("/menu/add")
    @Override
    public ResultBody addMenu(@ModelAttribute SystemMenu menu) {
        Boolean result = bizService.addMenu(menu);
        return ResultBody.success(result);
    }

    /**
     * 编辑菜单资源
     *
     * @param menuCode 菜单编码
     * @param title    菜单名称
     * @param icon     图标
     * @param prefix   请求前缀
     * @param path     请求路径
     * @param target   打开方式
     * @param status   是否启用
     * @param parentId 父节点ID
     * @param priority 优先级越小越靠前
     * @param menuDesc 描述
     * @return
     */
    @ApiOperation(value = "编辑菜单资源", notes = "编辑菜单资源")
    @ApiImplicitParams({@ApiImplicitParam(name = "menuId", required = true, value = "菜单ID", paramType = "form"), @ApiImplicitParam(name = "menuCode", required = true, value = "菜单编码", paramType = "form"), @ApiImplicitParam(name = "title", required = true, value = "菜单名称", paramType = "form"), @ApiImplicitParam(name = "icon", required = false, value = "图标", paramType = "form"), @ApiImplicitParam(name = "prefix", required = false, value = "请求路径", allowableValues = "/,http://,https://", paramType = "form"), @ApiImplicitParam(name = "path", required = false, value = "请求路径", paramType = "form"), @ApiImplicitParam(name = "target", required = false, value = "请求路径", allowableValues = "_self,_blank", paramType = "form"), @ApiImplicitParam(name = "parentId", required = false, defaultValue = "0", value = "父节点ID", paramType = "form"), @ApiImplicitParam(name = "status", required = true, defaultValue = "1", allowableValues = "0,1", value = "是否启用", paramType = "form"), @ApiImplicitParam(name = "priority", required = false, value = "优先级越小越靠前", paramType = "form"), @ApiImplicitParam(name = "menuDesc", required = false, value = "描述", paramType = "form"),})
    @PostMapping("/menu/update")
    @Override
    public ResultBody updateMenu(@RequestParam("menuId") Long menuId, @RequestParam(value = "menuCode") String menuCode, @RequestParam(value = "title") String title, @RequestParam(value = "icon", required = false) String icon, @RequestParam(value = "prefix", required = false, defaultValue = "/") String prefix, @RequestParam(value = "path", required = false, defaultValue = "") String path, @RequestParam(value = "target", required = false, defaultValue = "_self") String target, @RequestParam(value = "status", defaultValue = "1") Integer status, @RequestParam(value = "parentId", required = false, defaultValue = "0") Long parentId, @RequestParam(value = "priority", required = false, defaultValue = "0") BigDecimal priority, @RequestParam(value = "menuDesc", required = false, defaultValue = "") String menuDesc) {
        SystemMenu menu = new SystemMenu();
        menu.setId(menuId);
        menu.setName(menuCode);
        menu.setTitle(title);
        menu.setIcon(icon);
        menu.setPath(path);
        menu.setPrefix(prefix);
        menu.setTarget(target);
        menu.setStatus(status);
        menu.setParentId(parentId);
        menu.setPriority(priority);
        menu.setMenuDesc(menuDesc);
        Boolean result = bizService.updateMenu(menu);
        return ResultBody.success(result);
    }

    /**
     * 更新状态
     *
     * @param menuId 菜单ID
     * @return
     */
    @ApiOperation(value = "更新菜单资源状态", notes = "更新菜单资源状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "menuId", required = true, value = "menuId", paramType = "form"), @ApiImplicitParam(name = "status", required = true, defaultValue = "1", allowableValues = "0,1", value = "是否启用", paramType = "form")})
    @PostMapping("/menu/update/status")
    @Override
    public ResultBody updateStatus(@RequestParam("menuId") Long menuId, @RequestParam(value = "status", defaultValue = "1") Integer status) {
        Boolean result = bizService.updateStatus(menuId, status);
        return ResultBody.success(result);
    }

    /**
     * 移除菜单
     *
     * @param menuId 菜单ID
     * @return
     */
    @ApiOperation(value = "移除菜单资源", notes = "移除菜单资源")
    @ApiImplicitParams({@ApiImplicitParam(name = "menuId", required = true, value = "menuId", paramType = "form"),})
    @PostMapping("/menu/remove")
    @Override
    public ResultBody removeMenu(@RequestParam("menuId") Long menuId) {
        Boolean result = bizService.removeMenu(menuId);
        return ResultBody.success(result);
    }

    /**
     * 获取用户菜单
     *
     * @return
     */
    @ApiOperation(value = "获取用户菜单", notes = "获取用户菜单")
    @GetMapping("/menu/selectMenuByUserId")
    @Override
    public ResultBody selectMenuByUserId() {
        Long userId = OpenHelper.getUserAuth().getUserId();
        List<SystemMenu> allResources = bizService.selectMenuByUserId(userId);
        return ResultBody.success(allResources);
    }

    /**
     * 获取全部菜单
     *
     * @return
     */
    @ApiOperation(value = "获取全部菜单", notes = "获取全部菜单")
    @GetMapping("/menu/selectAllMenu")
    @Override
    public ResultBody selectAllMenu(@RequestParam("roleId") Long roleId) {
        List<SystemMenu> allResources = bizService.selectMenu(roleId);
        return ResultBody.success(allResources);
    }
}
